<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/31
 * Time: 11:02
 */

namespace app\controllers;

use app\lib\BaseFunction;
use app\models\CouponModal;
use app\models\Customer;
use app\models\CustomerRefer;
use app\models\Staff;
use yii;

class CustomerController extends ApiBaseController
{
    public function actionList()
    {
        $p                        = yii::$app->request->get('p', 1);
        $s                        = yii::$app->request->get('s', 10);
        $where['customer_name']   = trim(yii::$app->request->get('customerName', ''));
        $where['property']        = yii::$app->request->get('property');
        $where['refer']           = yii::$app->request->get('refer');
        $where['customer_group']  = yii::$app->request->get('customer_group');
        $where['follow_man']      = yii::$app->request->get('follow_man');
        $where['customer_status'] = yii::$app->request->get('customer_status');
        $where['email']           = trim(yii::$app->request->get('email'));
        $where['number']          = trim(yii::$app->request->get('number'));
        $where['from_website']    = yii::$app->request->get('from_website', 0);
        if ($this->user['role'] != 1 && $this->user['role'] != 2) {
            $where['follow_man'] = $this->user['job_number'];
        }
        $model = new Customer();
        $list  = $model->get_customer_list($p, $s, $where);
        $size  = $model->get_customer_size($where);
        if ($where['from_website'] == 2) {
            $customerId = array_column($list, 'id');
            $model      = new yii\db\Query();
            $arr        = $model->select('user_id')->from('jingsourcing_new_inquiry')
                ->where(['in', 'user_id', $customerId])->all();
            foreach ($list as $k => $v) {
                $list[$k]['have_inquiry'] = 0;
                foreach ($arr as $v2) {
                    if ($v['id'] == $v2['user_id']) {
                        $list[$k]['have_inquiry'] = 1;
                    }
                }
            }
        }
        return json_encode(['list' => $list, 'total' => $size]);
    }

    public function actionCustomerBase()
    {
        $model         = new CustomerRefer();
        $list          = $model->list();
        $data['refer'] = $list;
        $model         = new Staff();
        $data['staff'] = $model->get_all_saleman();
        return json_encode($data);
    }

    public function actionRefer()
    {
        $model         = new CustomerRefer();
        $list          = $model->list();
        $data['list']  = $list;
        $data['total'] = count($list);
        return json_encode($data);
    }

    public function actionAddrefer()
    {
        if (!yii::$app->request->isPost) {
            return json_encode(['code' => 0, 'msg' => '请求方式不正确']);
        }

        $name  = yii::$app->request->post('name');
        $model = new CustomerRefer();
        $model->add(['name' => $name]);
        return json_encode(['code' => 1, 'msg' => '添加成功']);
    }

    public function actionAdd()
    {
        if (!yii::$app->request->isPost) {
            return \json_encode(['code' => 0, 'msg' => '请求方式不正确']);
        }
        $data = yii::$app->request->post('data');
        if (!$data) {
            return \json_encode(['code' => 1, 'msg' => '参数错误']);
        }
        $data = \json_decode($data, true);
        if (!$data['email'] || !$data['customer_name']) {
            return \json_encode(['code' => 0, 'msg' => '数据错误']);
        }
        $model = new Customer();
        $re    = $model->one('*', ['email' => $data['email']]);
        if ($re) {
            return \json_encode(['code' => 0, 'msg' => '该邮箱已存在']);
        }
        //创建一个编号
        $data['follow_man'] = "[\"" . $data['follow_man'] . '"]';
        $data['number']     = $model->generateNum();
        $data['create_at']  = date('Y-m-d');
        $data['time']       = time();
        $re                 = $model->insert($data);
        $data               = $re ? ['code' => 1, 'msg' => '添加成功'] : ['code' => 0, 'msg' => '添加失败,请重试'];
        return \json_encode($data);
    }

    /**更新记录
     * @return string
     * @throws yii\db\Exception
     */
    public function actionUpdate()
    {
        $data = yii::$app->request->post();
        foreach ($data as $k => $v) {
            if (!in_array($k, ['id', 'value', 'column'])) {
                return json_encode(['code' => 0, 'msg' => '参数不正确']);
            }
        }
        $re = yii::$app->db->createCommand()
            ->update('jingsourcing_customer', [$data['column'] => $data['value']], ['id' => $data['id']])
            ->execute();
        if ($re) {
            return json_encode(['code' => 1, 'msg' => '修改成功']);
        } else {
            return json_encode(['code' => 0, 'msg' => '修改失败,请重试']);
        }
    }

    /**
     * 删除记录
     */
    public function actionDel()
    {
        $data = yii::$app->request->post();
        foreach ($data as $k => $v) {
            if (!in_array($k, ['id'])) {
                return json_encode(['code' => 0, 'msg' => '参数不正确']);
            }
        }
        if ($this->user['role'] != 1 && $this->user['role'] != 2) {
            return json_encode(['code' => 0, 'msg' => "暂无权限"]);
        }
        $re = yii::$app->db->createCommand()
            ->delete('jingsourcing_customer', ['id' => $data['id']])
            ->execute();
        if ($re) {
            return json_encode(['code' => 1, 'msg' => '删除成功']);
        } else {
            return json_encode(['code' => 0, 'msg' => '删除失败,请重试']);
        }
    }

    /**
     * mike介绍的客户需要进行审核
     */
    public function actionVerify()
    {
        if (!yii::$app->request->isPost) {
            return \json_encode(['code' => 0, 'msg' => 'method not allowed']);
        }

        $id = yii::$app->request->post('id');
        if (!is_numeric($id) || $id < 0) {
            return \json_encode(['code' => 0, 'msg' => '数据错误']);
        }

        BaseFunction::check_edit_auth($this->user['role']);

        $model = new Customer();
        //获取客户信息
        $info = $model->one('*', ['id' => $id]);
        if ($info['is_verify'] == 2) {
            return \json_encode(['code' => 0, 'msg' => '已经审核过了']);
        }
        $re = $model->updateRecord(['is_verify' => 2], $id);
        //不同客户介绍发放不同优惠券
        if ($re) {
            $model = new CouponModal();
            //更新成功发放优惠券
            if ($info['new_recommend'] == 's0n3') {
                $model->register50($id);
            }
        }

        $output = $re ? ['code' => 1, 'msg' => '更新成功'] : ['code' => 0, 'msg' => '更新失败'];
        return \json_encode($output);
    }
}
